package com.qilu.pe.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.ImageUrl;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.util.GlobalReqUtil;
import com.qilu.pe.support.util.Logg;
import com.qilu.pe.support.util.ViewUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.AppCompatEditText;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;

// 添加成员,医生
public class AddDoctorActivity extends Base2Activity {
    private AppCompatEditText et_name, et_year, et_id, et_phone, et_pwd;
    private RadioButton rb_1;// 男选中
    private TextView tv_ok;
    private TextView tv_name0, tv_sex0, tv_id0, tv_phone0, tv_pwd0;
    private RelativeLayout rl_header;
    private RoundedImageView iv_header;

    private String picture, name, sex, year, id, phone, pwd;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_add_doctor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        et_name = findViewById(R.id.et_name);
        et_year = findViewById(R.id.et_year);
        et_id = findViewById(R.id.et_id);
        et_phone = findViewById(R.id.et_phone);
        et_pwd = findViewById(R.id.et_pwd);
        rb_1 = findViewById(R.id.rb_1);
        tv_ok = findViewById(R.id.tv_ok);
        tv_name0 = findViewById(R.id.tv_name0);
        tv_sex0 = findViewById(R.id.tv_sex0);
        tv_id0 = findViewById(R.id.tv_id0);
        tv_phone0 = findViewById(R.id.tv_phone0);
        tv_pwd0 = findViewById(R.id.tv_pwd0);
        rl_header = findViewById(R.id.rl_header);
        iv_header = findViewById(R.id.iv_header);

        ViewUtil.setImportantText(tv_name0, "*姓名");
        ViewUtil.setImportantText(tv_sex0, "*性别");
        ViewUtil.setImportantText(tv_id0, "*身份证号");
        ViewUtil.setImportantText(tv_phone0, "*手机号");
        ViewUtil.setImportantText(tv_pwd0, "*密码");

        setHeadTitle("添加成员");

        setOnClickListener(rl_header, tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {
            name = et_name.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort("请填写姓名");
                return;
            }
            sex = rb_1.isChecked() ? "0" : "1";
            year = et_year.getText().toString().trim();
            id = et_id.getText().toString().trim();
            if (TextUtils.isEmpty(id)) {
                ToastUtils.showShort("请填写身份证号");
                return;
            }
            phone = et_phone.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                ToastUtils.showShort("请输入手机号");
                return;
            }
            pwd = et_pwd.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            hideProgress();
            reqAddDoctor();
            showProgress();
        }
        if (v == rl_header) {
            chooseImage();
        }
    }

    private void chooseImage() {
        Intent intent = new Intent(this, ImageGridActivity.class);
        startActivityForResult(intent, 1901);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 1901) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images.size() > 0) {
                    Logg.i("images.get(0).uri.getPath() = " + images.get(0).uri.getPath());
                    MultipartBody body = GlobalReqUtil.generateMultipartBody("id", new File(images.get(0).uri.getPath()));
                    GlobalReqUtil.reqUploadImage(body, new GlobalReqUtil.OnReqCallBack() {
                        @Override
                        public void onReqResult(BaseResult2<List<ImageUrl>> baseResult2) {
                            hideProgress();
                            if (baseResult2.isSuccess()) {
                                picture = baseResult2.getData().get(0).getImg();
                                GlideUtil.loadImg(Config.URL_IMG + picture, iv_header);
                            } else {

                            }
                            ToastUtils.showShort(baseResult2.getMsg());
                        }

                        @Override
                        public void onReqException(Throwable throwable) {
                            hideProgress();
                            throwable.printStackTrace();
                        }
                    });
                }
            } else {
                ToastUtils.showShort("没有数据");
            }
        }
    }

    private Disposable reqAddDoctor() {
        return APIRetrofit.getDefault().reqAddDoctor(Global.HEADER, picture, pwd, name, sex, id, phone, year)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            finish();
                        } else {

                        }
                        ToastUtils.showShort(result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }
}